﻿/*
COPYRIGHT (C) 2011-2013 Raynald Messié

THIS FILE IS PART OF SCRUMPILOT.

SCRUMPILOT IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUMPILOT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUMPILOT. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using ScrumPilot.Domain.Models;
using ScrumPilot.TFSHelpers;

namespace ScrumPilot.Domain.BPL
{
    public class SprintsServices : BusinessProcessLayer
    {
        public SprintsServices(TFSServices tfs)
            : base(tfs)
        {
        }

        public List<SprintForTable> GetSprints()
        {
            using (var unitOfWork = new UnitOfWork())
            {
                var m = unitOfWork.SprintRepository.GetSprints();
                var lstRet = new List<SprintForTable>(m.Count);
                lstRet.AddRange(m.Select(p => new SprintForTable
                                                  {
                                                      ID = p.sprintid,
                                                      Name = p.name,
                                                      StartDate = p.startdate,
                                                      EndDate = p.enddate,
                                                      State = (SprintState)p.state,
                                                      Iterations = p.sprintprojectiterations.Aggregate(String.Empty, (current, sij) => current + (" " + sij.currentsprint))
                                                  }));

                return lstRet;
            }
        }

        public Sprint GetSprint(int id)
        {
            if (id >= 0)
            {
                using (var unitOfWork = new UnitOfWork())
                {
                    var p = unitOfWork.SprintRepository.GetById(id);

                    var item = new Sprint
                        {
                            ID = id,
                            Name = p.name,
                            Description = p.description,
                            Retrospective = p.retrospective,
                            StartDate = p.startdate,
                            EndDate = p.enddate,
                            State = (SprintState)p.state,

                        };
                    return item;
                }
            }
            // a new sprint
            return new Sprint { State = SprintState.Next };
        }

        public Boolean SetSprint(Sprint model)
        {
            using (var unitOfWork = new UnitOfWork())
            {
                var mySprint = (model.ID == -1) ? new sprint() : unitOfWork.SprintRepository.GetById(model.ID);
                mySprint.name = model.Name;
                mySprint.description = model.Description;
                mySprint.retrospective = model.Retrospective;
                mySprint.startdate = model.StartDate;
                mySprint.enddate = model.EndDate;
                mySprint.state = (byte)model.State;
                if (model.ID == -1)
                    unitOfWork.SprintRepository.Add(mySprint);
                else
                    unitOfWork.SprintRepository.Update(mySprint);
                return unitOfWork.Save() > 0;
            }
        }

        public List<SprintIteration> GetFollowedCurrentIteration(int sprintId)
        {
            using (var unitOfWork = new UnitOfWork())
            {
                var fpid = this.FollowedProjectsIterationDictonary;
                var mySprint = unitOfWork.SprintRepository.GetById(sprintId);
                return fpid.Select(fp => new SprintIteration
                                             {
                                                 ID = fp.Key.ToString(CultureInfo.InvariantCulture),
                                                 Name = fp.Value,
                                                 Selected = mySprint.sprintprojectiterations.Count(p => p.projectid == fp.Key && p.project.currentsprint == p.currentsprint) > 0
                                             }).ToList();
            }
        }

        public Boolean SetIterations(int sprintId, int[] selectedIterations)
        {
            // delete all iterations for a sprint 
            using (var unitOfWork = new UnitOfWork())
            {
                var mySprint = unitOfWork.SprintRepository.GetById(sprintId);
                if (mySprint.sprintprojectiterations.Count > 0)
                {
                    mySprint.sprintprojectiterations.Clear();
                }
                foreach (var iteration in selectedIterations)
                {
                    mySprint.sprintprojectiterations.Add(new sprintprojectiteration
                        {
                            currentsprintid = sprintId,
                            projectid = iteration,
                            currentsprint = unitOfWork.ProjectRepository.GetById(iteration).currentsprint
                        });
                }
                return unitOfWork.Save() > 0;
            }
        }

    }
}
